package projectManagement;

import javax.swing.*;

import java.awt.*;

import ch.randelshofer.quaqua.util.Methods;
import ch.randelshofer.quaqua.*;
import ch.randelshofer.*;

import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;

import messages.*;
import java.util.*;
import projectManagementServer.*;

import java.io.*;

public class ClientGUI extends JPanel implements ActionListener, WindowListener{
	JFrame frame;
	JPanel panelLeft;
	JPanel panelCenter;
	static JPanel panelRight;

	static JPanel panelProfile;
	static ProfileButton profileBtn;
	static String profilePicName;
	static JButton btnLogIn;
	JPanel panelOption;

	JPanel panelDiscussion;
	JPanel panelInput;
	JPanel emSenPanel;
	static Editor editor;

	JPanel currentProject;
	static JLabel lblProject;
	JTabbedPane selectionTab;
	JPanel projectTab;
	JPanel fileTab;
	JPanel memberTab;

	static ArrayList<Member> allMember;
	static ArrayList<MemberProject> myProjectMember;
	static ArrayList<Project> allMyProject;
	static ArrayList<Project> completedProj;
	static ArrayList<Project> ongoingProj;
	static ArrayList<Files> allFiles;

	static ArrayList<Project> myAssignedProject;
	static ArrayList<MemberProject> allMemberProject;

	static DefaultListModel<String> projectListModel;
	static DefaultListModel<String> fileListModel;
	static DefaultListModel<String> memberListModel;

	static JList<String> projectList;
	JList<String> fileList;
	JList<String> memberList;

	private static JTextField textName;

	static actionsGUI acGUI;

	private static String userName = " ";
	private static String password = " ";
	static ClientLogic	clientLogic;
	private JTextField textIP;
	private JTextField textPort;
	private static JTextField textUserName;
	private static JPasswordField passwordField;

	static int version = 0;

	public ClientGUI() {
		setBackground(SystemColor.inactiveCaption);
		setPreferredSize(new Dimension(1000, 600));
		setLayout(null);
		setOpaque(true);

		allMember = new ArrayList<>();
		myProjectMember = new ArrayList<>();
		completedProj = new ArrayList<>();
		ongoingProj = new ArrayList<>();
		allFiles = new ArrayList<>();

		allMyProject = new ArrayList<>();
		myAssignedProject = new ArrayList<>();
		allMemberProject = new ArrayList<>();

		acGUI = new actionsGUI();

		panelLeft = new JPanel();
		panelLeft.setLayout(null);
		panelLeft.setOpaque(true);
		panelLeft.setBackground(SystemColor.menu);
		panelLeft.setBounds(5, 61, 178, 528);
		add(panelLeft);

		addProfilePanel();
		addOptions();
		addCredentialPanel();

		panelCenter = new JPanel();
		panelCenter.setLayout(null);
		panelCenter.setOpaque(true);
		panelCenter.setBackground(new Color(0, 0, 51));
		panelCenter.setBounds(187, 61, 603, 528);
		add(panelCenter);

		addDiscussionPanel();
		addInputPanel();
		addEmSenPanel();

		panelRight = new JPanel();
		panelRight.setLayout(null);
		panelRight.setOpaque(true);
		panelRight.setBackground(SystemColor.menu);
		panelRight.setBounds(793, 61, 203, 528);
		add(panelRight);

		addProjLabelPanel();
		addProjLabel();
		addSelectionTab();
		addProjTab();
		addFileTab();
		addMemberTab();

/****************************************************************************************************************************************************/

		JPanel panelBanner = new JPanel();
		panelBanner.setBackground(new Color(70, 130, 180));
		panelBanner.setOpaque(true);
		panelBanner.setBounds(5, 0, 991, 62);
		add(panelBanner);
		panelBanner.setLayout(null);

		JLabel lblLblbanner = new JLabel("");
		lblLblbanner.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(ServerGUI.class.getResource("/profile/BANNER.jpg"))));
		lblLblbanner.setBounds(132, 0, 700, 62);
		panelBanner.add(lblLblbanner);

		JPanel panelFooter = new JPanel();
		panelFooter.setBounds(0, 590, 1000, 10);
		add(panelFooter);

		frame = new JFrame();
		frame.setSize(1000, 680);
		frame.setVisible(true);
		frame.setResizable(true);
		frame.getContentPane().add(this);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


	}

/******************************************************************************************************************************************************/
/****************************************************************** LEFT COMPONENTS *******************************************************************/
/******************************************************************************************************************************************************/

// LEFT PANEL CONTENTS--------------------------------------------------------------------------------------------------------------------------

	public void addProfilePanel(){
		panelProfile = new JPanel();
		panelProfile.setBorder(new TitledBorder(null, "PROFILE", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelProfile.setBounds(10, 11, 158, 186);
		panelProfile.setLayout(null);

		profileBtn = new ProfileButton();

		profileBtn.setBackground(new Color(173, 216, 230));
		profileBtn.setBounds(10, 23, 138, 102);
		profileBtn.setActionCommand("Change Pic");
		profileBtn.addActionListener(acGUI);
		profilePicName = "/profile/profile.png";
		panelProfile.add(profileBtn);
		panelLeft.add(panelProfile);

		textName = new JTextField();
		textName.setEditable(false);
		textName.setHorizontalAlignment(SwingConstants.CENTER);
		textName.setText("Name");
		textName.setBounds(10, 139, 138, 36);
		textName.putClientProperty("Quaqua.showNonEditableCaret", true);
		panelProfile.add(textName);
		textName.setColumns(10);
	}

	public void addOptions(){
		panelOption = new JPanel();
		panelOption.setBorder(new TitledBorder(null, "OPTIONS", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelOption.setBounds(10, 208, 158, 129);
		panelLeft.add(panelOption);
		panelOption.setLayout(null);

		JButton btnEmoticons = new JButton("Emoticons");
		btnEmoticons.setBounds(25, 25, 99, 42);
		btnEmoticons.putClientProperty("Quaqua.Button.style", "bevel");
		btnEmoticons.addActionListener(acGUI);
		panelOption.add(btnEmoticons);

		JButton btnEditProfile = new JButton("Edit Profile");
		btnEditProfile.setBounds(25, 66, 99, 42);
		btnEditProfile.putClientProperty("Quaqua.Button.style", "bevel");
		btnEditProfile.addActionListener(acGUI);
		panelOption.add(btnEditProfile);
	}

	public void addCredentialPanel(){
		JPanel panelCredential = new JPanel();
		panelCredential.setBackground(SystemColor.scrollbar);
		panelCredential.setOpaque(true);
		panelCredential.setBorder(new TitledBorder(null, "CREDENTIAL", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		panelCredential.setBounds(10, 342, 158, 186);
		panelLeft.add(panelCredential);
		panelCredential.setLayout(null);

		JLabel lblIp = new JLabel("IP");
		lblIp.setBounds(6, 22, 50, 14);
		panelCredential.add(lblIp);

		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(92, 22, 56, 14);
		panelCredential.add(lblPort);

		textIP = new JTextField();
		textIP.setBounds(6, 35, 86, 31);
		panelCredential.add(textIP);
		textIP.setColumns(10);

		textPort = new JTextField();
		textPort.setBounds(89, 35, 63, 31);
		panelCredential.add(textPort);
		textPort.setColumns(10);

		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(6, 71, 86, 14);
		panelCredential.add(lblUsername);

		textUserName = new JTextField();
		textUserName.setBounds(6, 85, 146, 31);
		panelCredential.add(textUserName);
		textUserName.setColumns(10);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(6, 118, 86, 14);
		panelCredential.add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setBounds(6, 132, 146, 31);
		panelCredential.add(passwordField);

		btnLogIn = new JButton("LOG IN");
		btnLogIn.setBounds(35, 158, 89, 23);
		btnLogIn.putClientProperty("Quaqua.Button.style", "bevel");
		btnLogIn.addActionListener(this);
		panelCredential.add(btnLogIn);
	}

// CENTER PANEL CONTENTS------------------------------------------------------------------------------------------------------------------------

	public void addDiscussionPanel(){
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(10, 11, 583, 440);
		panel.setOpaque(true);
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Discussions", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(255, 255, 255)));
		panel.setLayout(new BorderLayout(0, 0));

		panelDiscussion = new JPanel();
		panelDiscussion.setLayout(new GridBagLayout());
		panelDiscussion.setOpaque(true);
		panelDiscussion.setBackground(Color.WHITE);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		scrollPane.setViewportView(panelDiscussion);

		panel.add(scrollPane, BorderLayout.CENTER);
		panelCenter.add(panel);
	}

	public void addInputPanel(){
		panelInput = new JPanel();
		panelInput.setOpaque(true);
		panelInput.setBounds(10, 456, 489, 65);
		panelInput.setLayout(new BorderLayout(0, 0));

		editor = new Editor();
		editor.setOpaque(true);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setViewportView(editor);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		panelInput.add(scrollPane, BorderLayout.CENTER);

		panelCenter.add(panelInput);
	}

	public void addEmSenPanel(){
		emSenPanel = new JPanel();
		emSenPanel.setOpaque(true);
		emSenPanel.setBounds(503, 457, 90, 64);
		panelCenter.add(emSenPanel);
		emSenPanel.setLayout(null);

		JButton btnSend = new JButton("Send");
		btnSend.setBounds(0, 0, 90, 64);
		btnSend.putClientProperty("Quaqua.Button.style", "bevel");
		btnSend.addActionListener(this);
		emSenPanel.add(btnSend);
	}

// RIGHT PANEL CONTENTS -----------------------------------------------------------------------------------------------------------------------------

	public void addProjLabelPanel(){
		currentProject = new JPanel();
		currentProject.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Project", TitledBorder.CENTER, TitledBorder.TOP, null, Color.BLACK));
		currentProject.setBounds(5, 11, 193, 44);
		panelRight.add(currentProject);
		currentProject.setLayout(null);
	}

	public void addProjLabel(){
		lblProject = new JLabel("New label");
		lblProject.setHorizontalAlignment(SwingConstants.CENTER);
		lblProject.setBounds(10, 18, 173, 20);
		currentProject.add(lblProject);
	}

	public void addSelectionTab(){
		selectionTab = new JTabbedPane(JTabbedPane.TOP);
		selectionTab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		selectionTab.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
		selectionTab.putClientProperty("Quaqua.TabbedPane.shortenTabs", Boolean.FALSE);
		selectionTab.setBounds(5, 66, 193, 457);
		panelRight.add(selectionTab);
	}

	public void addProjTab(){
		projectTab = new JPanel();
		projectTab.setBorder(null);
		projectTab.setLayout(null);
		selectionTab.addTab("Project", null, projectTab, null);

		projectListModel = new DefaultListModel<>();
		projectList = new JList<>();

		JPanel listHolder = new JPanel();
		listHolder.setBorder(new TitledBorder(null, "PROJECTS", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		listHolder.setBounds(0, 0, 183, 344);
		listHolder.setLayout(new BorderLayout(0, 0));

		projectListModel = new DefaultListModel<>();

		projectList = new JList<>();
		projectList.setFont(new Font("Vani", Font.PLAIN, 15));
		projectList.setModel(projectListModel);
		projectList.setSelectedIndex(-1);
		projectList.setVisibleRowCount(0);
		projectList.setValueIsAdjusting(true);
		projectList.setLocation(0,0);
		projectList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		projectList.putClientProperty( "Quaqua.List.style", "striped");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(projectList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		listHolder.add(scrollPane, BorderLayout.CENTER);

		JPanel optionPanel = new JPanel();
		optionPanel.setBackground(new Color(211, 211, 211));
		optionPanel.setOpaque(true);
		optionPanel.setOpaque(true);
		optionPanel.setBorder(null);
		optionPanel.setBounds(0, 344, 183, 84);
		optionPanel.setLayout(null);

		JLabel lblOption = new JLabel("Option:");
		lblOption.setBackground(new Color(211, 211, 211));
		lblOption.setOpaque(true);
		lblOption.setBounds(0, 0, 50, 14);
		optionPanel.add(lblOption);

		JButton btnConnect = new JButton("Connect");
		btnConnect.setBounds(0, 55, 87, 29);
		btnConnect.putClientProperty("Quaqua.Button.style", "bevel");
		btnConnect.addActionListener(this);
		optionPanel.add(btnConnect);

		projectTab.add(listHolder);
		projectTab.add(optionPanel);

		JButton btnAssignedProjects = new JButton("Assigned");
		btnAssignedProjects.setFont(new Font("Vani", Font.PLAIN, 12));
		btnAssignedProjects.setBounds(0, 13, 87, 29);
		btnAssignedProjects.putClientProperty("Quaqua.Button.style", "bevel");
		btnAssignedProjects.addActionListener(acGUI);
		optionPanel.add(btnAssignedProjects);

		JButton btnMemberedProjects = new JButton("Membered");
		btnMemberedProjects.setFont(new Font("Vani", Font.PLAIN, 12));
		btnMemberedProjects.setBounds(81, 13, 102, 29);
		btnMemberedProjects.putClientProperty("Quaqua.Button.style", "bevel");
		btnMemberedProjects.addActionListener(acGUI);
		optionPanel.add(btnMemberedProjects);

		JButton btnOtherOption = new JButton("Other Option");
		btnOtherOption.setBounds(81, 55, 102, 29);
		btnOtherOption.putClientProperty("Quaqua.Button.style", "bevel");
		btnOtherOption.addActionListener(acGUI);
		optionPanel.add(btnOtherOption);

		JButton btnAllProject = new JButton("ALL PROJECT");
		btnAllProject.setBounds(0, 36, 185, 25);
		btnAllProject.putClientProperty("Quaqua.Button.style", "bevel");
		btnAllProject.addActionListener(acGUI);
		btnAllProject.addActionListener(acGUI);
		optionPanel.add(btnAllProject);
	}

	public void addFileTab(){
		fileTab = new JPanel();
		fileTab.setBorder(null);
		selectionTab.addTab("File", null, fileTab, null);
		fileTab.setLayout(null);

		JPanel listHolder = new JPanel();
		listHolder.setBorder(new TitledBorder(null, "FILES", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		listHolder.setBounds(0, 0, 183, 355);
		listHolder.setLayout(new BorderLayout(0, 0));

		fileListModel = new DefaultListModel<>();

		fileList = new JList<>();
		fileList.setFont(new Font("Vani", Font.PLAIN, 15));
		fileList.setModel(fileListModel);
		fileList.setSelectedIndex(-1);
		fileList.setVisibleRowCount(0);
		fileList.setValueIsAdjusting(true);
		fileList.setLocation(0,0);
		fileList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		fileList.putClientProperty( "Quaqua.List.style", "striped");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(fileList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		listHolder.add(scrollPane, BorderLayout.CENTER);

		JPanel optionPanel = new JPanel();
		optionPanel.setBackground(new Color(211, 211, 211));
		optionPanel.setOpaque(true);
		optionPanel.setOpaque(true);
		optionPanel.setBorder(null);
		optionPanel.setBounds(0, 366, 183, 62);
		optionPanel.setLayout(null);

		JLabel lblOption = new JLabel("Option:");
		lblOption.setBackground(new Color(211, 211, 211));
		lblOption.setOpaque(true);
		lblOption.setBounds(3, 3, 75, 14);
		optionPanel.add(lblOption);

		JButton btnDownload = new JButton("Download");
		btnDownload.setBounds(0, 23, 90, 30);
		btnDownload.putClientProperty("Quaqua.Button.style", "bevel");
		btnDownload.addActionListener(acGUI);
		optionPanel.add(btnDownload);

		JButton btnUpload = new JButton("Upload");
		btnUpload.setBounds(93, 23, 90, 30);
		btnUpload.putClientProperty("Quaqua.Button.style", "bevel");
		btnUpload.addActionListener(acGUI);
		optionPanel.add(btnUpload);

		fileTab.add(listHolder);
		fileTab.add(optionPanel);
	}

	public void addMemberTab(){
		memberTab = new JPanel();
		memberTab.setBorder(null);
		selectionTab.addTab("Member", null, memberTab, null);
		memberTab.setLayout(null);

		JPanel listHolder = new JPanel();
		listHolder.setBorder(new TitledBorder(null, "MEMBERS", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		listHolder.setBounds(0, 0, 183, 355);
		listHolder.setLayout(new BorderLayout(0, 0));

		memberListModel = new DefaultListModel<>();

		memberList = new JList<>();
		memberList.setFont(new Font("Vani", Font.PLAIN, 15));
		memberList.setModel(memberListModel);
		memberList.setSelectedIndex(-1);
		memberList.setVisibleRowCount(0);
		memberList.setValueIsAdjusting(true);
		memberList.setLocation(0,0);
		memberList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		memberList.putClientProperty( "Quaqua.List.style", "striped");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(memberList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		listHolder.add(scrollPane, BorderLayout.CENTER);

		JPanel optionPanel = new JPanel();
		optionPanel.setBackground(new Color(211, 211, 211));
		optionPanel.setOpaque(true);
		optionPanel.setOpaque(true);
		optionPanel.setBorder(null);
		optionPanel.setBounds(0, 366, 183, 62);
		optionPanel.setLayout(null);

		JLabel lblOption = new JLabel("Option:");
		lblOption.setBackground(new Color(211, 211, 211));
		lblOption.setOpaque(true);
		lblOption.setBounds(3, 3, 75, 14);
		optionPanel.add(lblOption);

		JButton btnSendMessage = new JButton("Send Message");
		btnSendMessage.setBounds(0, 21, 183, 30);
		btnSendMessage.putClientProperty("Quaqua.Button.style", "bevel");
		optionPanel.add(btnSendMessage);

		memberTab.add(listHolder);
		memberTab.add(optionPanel);

	}


	public static void setLookAndFeel(){
		   System.setProperty("Quaqua.tabLayoutPolicy", "wrap");
		    if (!System.getProperty("os.name").toLowerCase().startsWith("mac") && !System.getProperty("os.name").toLowerCase().startsWith("darwin")) {
		      try {
		        Methods.invokeStatic(JFrame.class, "setDefaultLookAndFeelDecorated", Boolean.TYPE, Boolean.TRUE);
		        Methods.invokeStatic(JDialog.class, "setDefaultLookAndFeelDecorated", Boolean.TYPE, Boolean.TRUE);
		      }
		      catch (NoSuchMethodException e) {
		        e.printStackTrace();
		      }
		    }
		    try {
		      UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
	}


	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					setLookAndFeel();
					new ClientGUI();
				} catch (Exception e) {}
			}
		});

	}

	/******************************************************************************************************************************************************/
	/****************************************************************** ACTION PERFORM *******************************************************************/
	/******************************************************************************************************************************************************/

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		if(cmd.equalsIgnoreCase("Change Pic")){
			new ClientProfilePictureSelection();
			profileBtn.addActionListener(this);
		}

		if(cmd.equalsIgnoreCase("LOG IN")){
			if(textUserName.getText() != null & passwordField.getPassword() != null & textIP.getText() != null & textPort.getText() != null){
				userName = textUserName.getText();
				panelDiscussion.removeAll();
				panelDiscussion.revalidate();
				panelDiscussion.repaint();
				clientLogic = new ClientLogic(this, textIP.getText());
				clientLogic.start();
				textName.setText(textUserName.getText());

				textUserName.setText("*********");
				passwordField.setText("********");
				textIP.setEditable(false);
				textPort.setEditable(false);
				textUserName.setEnabled(false);
				passwordField.setEnabled(false);
				btnLogIn.setText("LOG OUT");
			}else{
				JOptionPane.showMessageDialog(null, "Invalid Parameter");
			}
		}

		if(cmd.equalsIgnoreCase("LOG OUT")){
			resetAll();
			textUserName.setText("");
			passwordField.setText("");
			textIP.setEditable(true);
			textPort.setEditable(true);
			textUserName.setEnabled(true);
			passwordField.setEnabled(true);
			btnLogIn.setText("LOG IN");
			JOptionPane.showMessageDialog(null, "You are now LOG OUT");
		}

		if(cmd.equalsIgnoreCase("Send")){
			Message msg =  new DiscussionMessage(textName.getText(), profilePicName, editor.getText(), editor.getHeight(), lblProject.getText());
			clientLogic.sendMsg(msg);
		}

		if(cmd.equalsIgnoreCase("Connect")){
			lblProject.setText(projectListModel.getElementAt(projectList.getSelectedIndex()));
			if(!projectList.isSelectionEmpty()){
				for(int i=0; i<allFiles.size(); i++){
					if(lblProject.getText().equalsIgnoreCase(allFiles.get(i).getProjectName())){
						fileListModel.addElement(allFiles.get(i).getFileName());
					}
				}
				Message msg = new ConnectMessage(projectListModel.getElementAt(projectList.getSelectedIndex()), lblProject.getText());
				clientLogic.sendMsg(msg);

			}else{
				JOptionPane.showMessageDialog(null, "Please Select a Project");
			}

		}


	}

	public class actionsGUI implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent btn) {
			String command = btn.getActionCommand();

			if(command.equalsIgnoreCase("Change Pic")){
				new ClientProfilePictureSelection();
			}

			if(command.equalsIgnoreCase("Emoticons")){
				new Emoticons();
			}


			if(command.equalsIgnoreCase("Other Option")){
				int token = 0;
				String projectStatus = " ";
				if(!projectList.isSelectionEmpty()){
					for(int i=0; i<myAssignedProject.size(); i++){
						if(projectListModel.get(projectList.getSelectedIndex()).equalsIgnoreCase(myAssignedProject.get(i).getProjectName())){
							token++;
							projectStatus = myAssignedProject.get(i).getStatus();
						}
					}
					if(token > 0){
						new ProjectOptionGUI(projectStatus);
					}else{
						JOptionPane.showMessageDialog(null, "UnAuthorize");
					}

				}else{
					JOptionPane.showMessageDialog(null, "Please Select a Project");
				}


			}

			if(command.equalsIgnoreCase("Assigned")){
				projectListModel.clear();
				panelRight.validate();
				panelRight.repaint();

				for(Project x : myAssignedProject){
					projectListModel.addElement(x.getProjectName());
				}
			}

			if(command.equalsIgnoreCase("Membered")){
				projectListModel.clear();
				panelRight.validate();
				panelRight.repaint();

				for(MemberProject x : myProjectMember){
					projectListModel.addElement(x.getProjectName());
				}
			}

			if(command.equalsIgnoreCase("ALL PROJECT")){
				projectListModel.clear();
				panelRight.validate();
				panelRight.repaint();

				for(Project  x : allMyProject){
					projectListModel.addElement(x.getProjectName());
				}
			}

			if(command.equalsIgnoreCase("UPLOAD")){
				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(ClientGUI.this);
				String filePath = chooser.getSelectedFile().getAbsolutePath();
				String fileName = chooser.getSelectedFile().getName();
				String[] fileTypeConst = fileName.split("\\.");
				String fileType = fileTypeConst[0];

				File file = new File(chooser.getSelectedFile().getAbsolutePath());
				byte[] fileByte = new byte[(int) file.length()];

				fileListModel.addElement(fileName);
				try {
					FileInputStream fileInputStream = new FileInputStream(file);
					fileInputStream.read(fileByte);
					fileInputStream.close();

					Message sendFile = new FileMessage(fileName, textName.getText(), fileByte, fileType, lblProject.getText());
					clientLogic.sendMsg(sendFile);
				} catch (Exception e) {
					e.printStackTrace();
				}

				JOptionPane.showMessageDialog(null, "UPLOAD COMPLETE");
			}

			if(command.equalsIgnoreCase("DOWNLOAD")){
				FileOutputStream outputFile;
				for(int i=0; i<allFiles.size(); i++){
					if(allFiles.get(i).getFileName().equalsIgnoreCase(fileListModel.getElementAt(fileList.getSelectedIndex()))){
						try {
							File directory = new File("C:\\projectManagement");
							if(!directory.exists()){
								directory.mkdir();
								File file = new File("C:\\projectManagement\\"+allFiles.get(i).getFileName());
								if(!file.exists()){
									version = 0;
									file.createNewFile();
									outputFile = new FileOutputStream("C:\\projectManagement\\" + allFiles.get(i).getFileName());
									outputFile.write(allFiles.get(i).getFileCont());
									outputFile.close();
								}else{
									file.createNewFile();
									outputFile = new FileOutputStream("C:\\projectManagement\\" + allFiles.get(i).getFileName() + version++);
									outputFile.write(allFiles.get(i).getFileCont());
									outputFile.close();
								}
							}else{
								File file = new File("C:\\projectManagement\\"+allFiles.get(i).getFileName());
								if(!file.exists()){
									version = 0;
									file.createNewFile();
									outputFile = new FileOutputStream("C:\\projectManagement\\" + allFiles.get(i).getFileName());
									outputFile.write(allFiles.get(i).getFileCont());
									outputFile.close();
								}else{
									file.createNewFile();
									outputFile = new FileOutputStream("C:\\projectManagement\\" + allFiles.get(i).getFileName() + version++);
									outputFile.write(allFiles.get(i).getFileCont());
									outputFile.close();
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}


					}
				}
				JOptionPane.showMessageDialog(null, "DOWNLOAD COMPLETE");
			}
		}

	}

	/******************************************************************************************************************************************************/
	/****************************************************************** LEFT COMPONENTS *******************************************************************/
	/******************************************************************************************************************************************************/

	/************************************************ UPDATE PROFILE PICTURE *****************************************************************/
	public static void updateProfile(String picName){
		new ImageIcon(picName);
		profileBtn = new ProfileButton();
		profileBtn.setActionCommand("Change Pic");
		profileBtn.setBounds(10, 23, 138, 118);
		profileBtn.addActionListener(acGUI);
		profilePicName = picName;
		panelProfile.remove(0);
		panelProfile.repaint();
		panelProfile.add(profileBtn);
		panelProfile.add(textName);
	}

	/*********************************************** APPEND EMOTICONS *************************************************************************/
	public static void appendEmoticons(String emo){
		editor.setText(editor.getText()+emo+" ");
	}

// CONNECTION -----------------------------------------------------------------------------------------------------------------------------
	public void connectionFailed(){
		//connected = false;
	}

	public void setCredential(String user, String pass){
		userName = user;
		password = pass;
	}



// WINDOW ----------------------------------------------------------------------------------------------------------------------------------

	@Override
	public void windowClosing(WindowEvent e) {
		if(clientLogic != null){
			resetAll();
		}
		frame.dispose();
		System.exit(0);
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}

// LOG IN -------------------------------------------------------------------------------------------------------------------------------
	public String getUserNameCredential(){
		return textUserName.getText();
	}

	public String getPasswordCredential(){
		return String.valueOf(passwordField.getPassword());
	}

	public String getIP(){
		return textIP.getText();
	}

	public int getPort(){
		return Integer.parseInt(textPort.getText());
	}

	public void logInSuccess(){
		JOptionPane.showMessageDialog(null, "WELCOME!");
		panelRight.validate();
		panelRight.repaint();
		for(Project x : ongoingProj){
			projectListModel.addElement(x.getProjectName());
		}

	}

	public void logInError(String err){

		clientLogic = null;
		btnLogIn.setText("LOG IN");
		panelRight.revalidate();
		panelRight.repaint();
		projectListModel.clear();
		fileListModel.clear();
		memberListModel.clear();

		allMember = new ArrayList<>();
		myProjectMember = new ArrayList<>();
		allMyProject = new ArrayList<>();
		completedProj = new ArrayList<>();
		ongoingProj = new ArrayList<>();
		allFiles = new ArrayList<>();

		textUserName.setText("");
		textUserName.setEnabled(true);

		passwordField.setText("");
		passwordField.setEnabled(true);
		JOptionPane.showMessageDialog(null, err);
	}


// APPEND MSG --------------------------------------------------------------------------------------------------------------------------------
	public void appendDiscussionMsg(Message msg){
		PackMessage packMessage = new PackMessage(msg.getUser(), msg.getAvatar(), msg.getMsg(), msg.getTime(), msg.getMsgLength());

		GridBagConstraints gbc = new GridBagConstraints();
		JPanel comradeMsg = packMessage.getInstance();
		comradeMsg.setOpaque(true);

		gbc.gridx = 0;
		gbc.gridy = panelDiscussion.getComponentCount();

		panelDiscussion.revalidate();
		panelDiscussion.repaint();

		panelDiscussion.add(comradeMsg,gbc);
	}

//APPEND DATA ---------------------------------------------------------------------------------------------------------------------------------

	public void setAllMember(ArrayList<Member> allMem){
		for(Member x : allMem){
			allMember.add(x);
		}
	}

	public void setMyMemProj(ArrayList<MemberProject> memProj){
		for(int x=0; x<memProj.size(); x++){
			if(memProj.get(x).getMember().equalsIgnoreCase(userName)){
				for(int i=0; i<ongoingProj.size(); i++){
					if(memProj.get(x).getProjectName().equalsIgnoreCase(ongoingProj.get(i).getProjectName())){
						//if(allMyProject.get(i).getStatus().equalsIgnoreCase("ONGOING")){
							myProjectMember.add(memProj.get(x));
							allMyProject.add(new Project(memProj.get(x).getProjectName(), "", "",""));
							projectListModel.addElement(memProj.get(x).getProjectName());
							//JOptionPane.showMessageDialog(null, memProj.get(i));
						//}
					}
				}
			}
		}
	}

	public void setAllMemberProject(ArrayList<MemberProject> allMemProj){
		for(MemberProject x : allMemProj){
			allMemberProject.add(x);
		}
	}

	public void setAllProject(ArrayList<Project> allProject){
		for(Project x : allProject){
			if(x.getProjectLeader().equalsIgnoreCase(userName)){
				panelRight.revalidate();
				panelRight.repaint();
				projectListModel.addElement(x.getProjectName());
				myAssignedProject.add(x);
				allMyProject.add(x);
			}
			if(x.getStatus().equalsIgnoreCase("ONGOING")){
				ongoingProj.add(x);
			}
			if(x.getStatus().equalsIgnoreCase("COMPLETED")){
				completedProj.add(x);
			}
		}
	}

	public void addLobbyAsProject(){
		Project lobby = new Project("LOBBY", null, null, null);
		allMyProject.add(lobby);
	}

	public void setFiles(ArrayList<Files> allFile){
		for(int i=0; i<allFile.size(); i++){
			allFiles.add(allFile.get(i));
		}

	}

	public void setLblProject(String currProj){
		panelRight.revalidate();
		panelRight.repaint();
		lblProject.setText(currProj);
	}


// UPDATE DATA --------------------------------------------------------------------------------------------------------------------
	public synchronized void updateMemberList(ArrayList<String> loggedMembers){
		//if(!(memberListModel.contains(loggedMember))){
			panelRight.revalidate();
			panelRight.repaint();
			memberListModel.clear();

			for(int i=0; i<loggedMembers.size(); i++){
				memberListModel.addElement(loggedMembers.get(i));
			}
			/**for(String x : loggedMembers){
				memberListModel.addElement(x);
				///JOptionPane.showMessageDialog(null, x);
			}**/
		//}

	}

	public synchronized void addSingleMember(Message msg){
		AddSingleMemberToList mem = (AddSingleMemberToList) msg;
		panelRight.revalidate();
		panelRight.repaint();
		memberListModel.addElement(mem.getMember());
	}
// UPDATE MEMBER LIST ----------------------------------------------------------------------------------------------------------------
	public void updateAllMemberFromList(Message msg){
		MemberMessage memberMessage = (MemberMessage) msg;
		memberListModel.clear();
		panelRight.revalidate();
		panelRight.repaint();

		for(int i=0 ; i<memberMessage.getAllMembers().size(); i++){
			memberListModel.addElement(memberMessage.getAllMembers().get(i));
		}
		/**for(String x : memberMessage.getAllMembers()){
			memberListModel.addElement(x);
		}**/
	}

//ADD MEMBER TO LIST -----------------------------------------------------------------------------------------------------------------
//?????
//?????
	public void addSpecificMemberToList(Message msg){
		AddSpecificMemberMessage message = (AddSpecificMemberMessage) msg;
		panelRight.revalidate();
		panelRight.repaint();
		memberListModel.clear();
		for(String x : message.getMemberList()){
			memberListModel.addElement(x);
		}

	}

//UPDATE DISUCCION PANEL -------------------------------------------------------------------------------------------------------------
	public synchronized void clearDiscussionPanel(){
		panelDiscussion.removeAll();
		panelDiscussion.revalidate();
		panelDiscussion.repaint();
	}

	public void addLobbyRoom(){
		panelRight.revalidate();
		panelRight.repaint();
		projectListModel.addElement("LOBBY");
	}

// REQUEST TO ADD MEMBER --------------------------------------------------------------------------------------------------------------
	public static ArrayList<String> getAllMembers(){
		ArrayList<String> existingMembers = getCurrentMembers();
		existingMembers.add(textName.getText());
		ArrayList<String> allMembers = new ArrayList<>();
		for(int i=0; i<allMember.size(); i++){
			if(!existingMembers.contains(allMember.get(i).getUserName())){
				allMembers.add(allMember.get(i).getUserName());
			}else{}

		}
		return allMembers;
	}

	public static ArrayList<String> getCurrentMembers(){
		ArrayList<String> currMember = new ArrayList<>();
		for(int i=0; i<allMemberProject.size(); i++){
			if(allMemberProject.get(i).getProjectName().equalsIgnoreCase(projectListModel.getElementAt(projectList.getSelectedIndex()))){
				currMember.add(allMemberProject.get(i).getMember());
			}
		}
		return currMember;
	}

	public static void sendAddMemberToProject(ArrayList<String> memberName){
		for(int i=0 ; i<memberName.size(); i++){
			MemberProject newMemProj = new MemberProject(lblProject.getText(), memberName.get(i));
			allMemberProject.add(newMemProj);
			AddMemberMessage member = new AddMemberMessage();
			member.setMemberToAdd(memberName.get(i));
			member.setProject(projectListModel.getElementAt(projectList.getSelectedIndex()));
			clientLogic.sendMsg(member);
		}
	}

	public static void sendDeleteMemberToProject(String memberName){
		for(int i=0; i<allMemberProject.size(); i++){
			if(allMemberProject.get(i).getProjectName().equalsIgnoreCase(lblProject.getText()) & allMemberProject.get(i).getMember().equalsIgnoreCase(memberName)){
				allMemberProject.remove(i);
			}
		}

		DeleteMemberMessage dmm = new DeleteMemberMessage();
		dmm.addMemberToDelete(memberName);
		dmm.addMemberProject(lblProject.getText());
		clientLogic.sendMsg(dmm);
	}

//SERVER STOP -------------------------------------------------------------------------------------------------------------------------
	public static void resetAll(){
		version = 0;
		btnLogIn.setText("LOG IN");
		clientLogic.disconnect(textName.getText(), lblProject.getText());
		clientLogic = null;
		panelRight.revalidate();
		panelRight.repaint();
		projectListModel.clear();
		fileListModel.clear();
		memberListModel.clear();

		allMember = new ArrayList<>();
		allMemberProject = new ArrayList<>();
		myProjectMember = new ArrayList<>();
		allMyProject = new ArrayList<>();
		completedProj = new ArrayList<>();
		ongoingProj = new ArrayList<>();
		allFiles = new ArrayList<>();

		textUserName.setText("");
		textUserName.setEnabled(true);

		passwordField.setText("");
		passwordField.setEnabled(true);

	}

//PROJECT COMPLETED --------------------------------------------------------------------------------------------------------------------
	public static void sendCompletedProject(){
		CompletedMessage cM = new CompletedMessage(lblProject.getText());
		clientLogic.sendMsg(cM);
	}

//PROJECT ONGOING -----------------------------------------------------------------------------------------------------------------------
	public static void sendOngoingProject(){
		OngoingMessage oM = new OngoingMessage(lblProject.getText());
		clientLogic.sendMsg(oM);
	}

//UPDATE LOCAL PROJECT STATUS
	public static void setLocalProjectStatus(String stat){
		for(int i=0; i<myAssignedProject.size(); i++){
			if(projectListModel.get(projectList.getSelectedIndex()).equalsIgnoreCase(myAssignedProject.get(i).getProjectName())){
				myAssignedProject.get(i).setProjectStatus(stat);
			}
		}
	}

}


